﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Utils;

public partial class forum_topic : FBasePage
{
    // Fields
    public Maticsoft.BLL.topic bbsbll = new Maticsoft.BLL.topic();
    public List<Maticsoft.Model.topic> bbslist = new List<Maticsoft.Model.topic>();


    public string pagenum = "1";
    public string strpage;
    public Maticsoft.Model.topic topicmodel = new Maticsoft.Model.topic();
    public Maticsoft.BLL.UserInfo userbll = new Maticsoft.BLL.UserInfo();
    public Maticsoft.Model.UserInfo usermodel = new Maticsoft.Model.UserInfo();

    // Methods
    public void AddReplay()
    {
        Maticsoft.BLL.UserInfo info = new Maticsoft.BLL.UserInfo();
        Maticsoft.BLL.UserInfo info2 = new Maticsoft.BLL.UserInfo();
        int id = Convert.ToInt32(this.hidbbsid.Value);
        string str = base.Request["content"];
        if ((str.IndexOf("<") != -1) || (str.IndexOf(">") != -1))
        {
            this.SetError("不允许输入非法参数，提交失败!");
        }
        else
        {
            int userid = info2.Getuserid(base.UserName);
            Maticsoft.BLL.topic topic = new Maticsoft.BLL.topic();
            Maticsoft.Model.topic model = new Maticsoft.Model.topic();
            model.Parent_id = id;
            model.Content = str;
            model.User_id = userid;
            model.Last_time = DateTime.Now;
            model.create_time = DateTime.Now;
            if (topic.Add(model) > 0)
            {
                base.SetSuccess("恭喜您，回复成功");
                this.update(id);
                this.ReplyUser();
            }
            else
            {
                this.SetError("error");
            }
        }
    }

    public void delete(int id)
    {
        new Maticsoft.BLL.topic().Delete(id);
    }

    public void deletebbs(int id)
    {
        new Maticsoft.BLL.topic().Delete(id);
    }

    public string Get_name(string ename)
    {
        Maticsoft.BLL.Category category = new Maticsoft.BLL.Category();
        return category.Get_name(this.Session["ename"].ToString());
    }

    public string Gettime(int id)
    {
        Maticsoft.BLL.topic topic = new Maticsoft.BLL.topic();
        Maticsoft.BLL.topic topic2 = new Maticsoft.BLL.topic();
        return topic2.GetModel(id).create_time.ToString();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        if (base.Request["pgnum"] != null)
        {
            if (Utility.IsNum(base.Request["pgnum"].ToString()))
            {
                this.pagenum = base.Request["pgnum"].ToString();
            }
            else
            {
                this.SetError("您输入的参数非法");
                base.Response.End();
                return;
            }
        }
        if (base.Request["del"] != null)
        {
            if (Utility.IsNum(base.Request["id"].ToString()))
            {
                this.delete(Convert.ToInt32(base.Request["del"].ToString()));
            }
            else
            {
                this.SetError("您输入的参数非法");
                base.Response.End();
                return;
            }
        }
        if (base.Request["id"] != null)
        {
            if (Utility.IsNum(base.Request["id"].ToString()))
            {
                this.hidbbsid.Value = base.Request["id"].ToString();
                this.topicUser();
                this.ReplyUser();
                this.view_update(Convert.ToInt32(base.Request["id"].ToString()));
            }
            else
            {
                this.SetError("您输入的参数非法");
                base.Response.End();
                return;
            }
        }
        if (base.Request["top"] != null)
        {
            if (Utility.IsNum(base.Request["id"].ToString()))
            {
                this.topbbs(Convert.ToInt32(base.Request["top"].ToString()));
            }
            else
            {
                this.SetError("您输入的参数非法");
                base.Response.End();
                return;
            }
        }
        if (base.Request["bbsid"] != null)
        {
            if (Utility.IsNum(base.Request["bbsid"].ToString()))
            {
                this.deletebbs(Convert.ToInt32(base.Request["bbsid"].ToString()));
                base.Response.Redirect("index.aspx");
            }
            else
            {
                this.SetError("您输入的参数非法");
                base.Response.End();
                return;
            }
        }
        if (base.Request.Form["commit"] == "发布")
        {
            if (!(this.Page.User.Identity.IsAuthenticated && !(this.Page.User.Identity.Name == "")))
            {
                base.Response.Redirect("/account/Login.aspx");
            }
            else
            {
                this.AddReplay();
            }
        }
    }

    public void ReplyUser()
    {
        int num;
        Maticsoft.BLL.topic topic = new Maticsoft.BLL.topic();
        this.bbslist = topic.Searchtopic(30, Convert.ToInt32(this.pagenum), "*", "create_time", " Parent_id=" + Convert.ToInt32(this.hidbbsid.Value), out num);
        if (this.bbslist.Count == 0)
        {
            this.strpage = "";
        }
        else if (num >= 30)
        {
            this.strpage = topic.GetBBsReplyPageStr(num, Convert.ToInt32(this.pagenum), 30, Convert.ToInt32(base.Request["id"].ToString()));
        }
    }

    public void topbbs(int id)
    {
        TimeSpan ts = new TimeSpan(DateTime.Parse("1990-09-13 18:47:45.043").Ticks);
        TimeSpan span2 = new TimeSpan(DateTime.Now.Ticks);
        int minutes = span2.Subtract(ts).Duration().Minutes;
        Maticsoft.BLL.topic topic = new Maticsoft.BLL.topic();
        Maticsoft.Model.topic model = new Maticsoft.Model.topic();
        model = topic.GetModel(id);
        if (model.Head != 0L)
        {
            topic.deletetop(id);
            base.SetSuccess("取消话题置顶成功");
        }
        else if (model.Head == 0L)
        {
            topic.updatetop(minutes, id);
            base.SetSuccess("设置话题置顶成功");
        }
    }

    public void topicUser()
    {
        this.topicmodel = this.bbsbll.GetModel(Convert.ToInt32(this.hidbbsid.Value));
    }

    public void update(int id)
    {
        new Maticsoft.BLL.topic().update(id);
    }

    public void view_update(int id)
    {
        new Maticsoft.BLL.topic().View_update(id);
    }

}